package com.parking.common.xxljob.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class XxljobUtil {
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.id}")
    private Integer executorId;

    public void addJob(LocalDateTime executeTime,Object id,String jobHandler,String jobDesc,String jobAuth) throws Exception {
        String xxlLoginAPI= adminAddresses+"/login";
        String xxlAddJobAPI= adminAddresses+"/jobinfo/add";
        HashMap<String, Object> loginParam = new HashMap<String,Object>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        if(httpResponse.getStatus()==200) {
            System.out.println("登录成功");
            Map<String, Object> addTaskParam = new HashMap<String,Object>();
            addTaskParam.put("jobGroup", executorId);

            int year = executeTime.get(ChronoField.YEAR);
            int month = executeTime.get(ChronoField.MONTH_OF_YEAR);
            int day = executeTime.get(ChronoField.DAY_OF_MONTH);
            int hour=executeTime.get(ChronoField.HOUR_OF_DAY);
            int minute=executeTime.get(ChronoField.MINUTE_OF_HOUR);
            int second=executeTime.get(ChronoField.SECOND_OF_MINUTE);
            StringBuilder cron = new StringBuilder();
            cron.append(second);
            cron.append(" ").append(minute);
            cron.append(" ").append(hour);
            cron.append(" ").append(day);
            cron.append(" ").append(month);
            cron.append(" ?");
            cron.append(" ").append(year);
            System.out.println(cron.toString());
            addTaskParam.put("scheduleConf", cron.toString());
            addTaskParam.put("jobDesc", jobDesc);
            addTaskParam.put("author", jobAuth);
            addTaskParam.put("executorHandler", jobHandler);
            addTaskParam.put("executorParam",id);//测试的拼团编号
            addTaskParam.put("scheduleType", "CRON");

            addTaskParam.put("executorRouteStrategy", "FIRST");
            addTaskParam.put("executorBlockStrategy", "SERIAL_EXECUTION");
            addTaskParam.put("glueType", "BEAN");
            addTaskParam.put("misfireStrategy","DO_NOTHING");
            addTaskParam.put("executorTimeout", "0");
            addTaskParam.put("executorFailRetryCount","0");
            addTaskParam.put("triggerStatus", 1);
            HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(addTaskParam).execute();
            System.out.println(response.body());
            //从body中取出content content代表任务ID
            //使用redis保存拼团编号-任务ID键值对
            if(response.getStatus()!=200) {
                throw new Exception("添加任务失败");
            }
        }
    }

    public void removeJob(long jobId){
        String xxlLoginAPI= adminAddresses+"/login";
        String xxlAddJobAPI= adminAddresses+"/jobinfo/remove";
        HashMap<String, Object> loginParam = new HashMap<String,Object>();
        loginParam.put("userName", "admin");
        loginParam.put("password", "123456");
        HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
        if(httpResponse.getStatus()==200) {
            HashMap<String, Object> delParam = new HashMap<String,Object>();
            delParam.put("id", jobId);
            HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(delParam).execute();
        }
    }
}
